*------------------------------------------------------------------------------*
						*** TABLE S4. ROBUSTNESS CHECKS ***
*------------------------------------------------------------------------------*

***PROGRAM FOR ESTABLISHED OPPOSITION AND OTHER PARTIES [COL. 5-6]
cap program drop reg_tab4_votes
program define reg_tab4_votes, sclass
	args cmd spec 
	
	// THE FOLLOWING LOOP RUNS THE ANALYSIS SEPARATELY FOR ALL DEPENDENT VARIABLES
	local Y : word count $depvars_tab4_votes // PICKS EACH VARIABLE ONE BY ONE FROM MACRO
	forvalues i=1/`Y' {
		local y : word `i' of $depvars_tab4_votes
		`cmd' `y' ${spec`spec'} // THIS RUNS THE REGRESSION
		estadd ysumm
		
		mat b = e(b) // THIS SAVES THE COEFFICIENT VECTOR
		scalar bPM10_`spec'_`i'=b[1,1] // PM10 coefficient
		
		mat V = e(V) // THIS SAVES THE VARIANCE MATRIX
		
		// THE FOLLOWING SAVES STANDARD ERRORS, R2 AND N AS SCALARS
		scalar sePM10_`spec'_`i'  =sqrt(V[1,1]) // PM10 SE
		scalar r2`spec'_`i'  = e(r2) // r-squared
		scalar N`spec'_`i'  = e(N) // N
		scalar YM`spec'_`i' = e(ymean)  // Mean Dep. Var.
		
		// THE FOLLOWING PRODUCES SIGNIFICANCE STARS AND SAVES THEM AS LOCALS
		local pPM10_`spec'_`i'=2*(1-normal(abs(bPM10_`spec'_`i'/sePM10_`spec'_`i')))
		local starPM10_`spec'_`i' ""
		if `pPM10_`spec'_`i''<.1 {	
			local starPM10_`spec'_`i' *
		}
		if `pPM10_`spec'_`i''<.05 {
			local starPM10_`spec'_`i' **
		}
		if `pPM10_`spec'_`i''<.01 {
			local starPM10_`spec'_`i' ***
		}
		global starPM10_`spec'_`i' = "`starPM10_`spec'_`i''"
	}  // i
end

*------------------------------------------------------------------------------*

***PROGRAM FOR IV [COL. 1]
cap program drop reg_tab4_IV
program define reg_tab4_IV, sclass
	args cmd spec 
	
	// THE FOLLOWING LOOP RUNS THE ANALYSIS SEPARATELY FOR ALL DEPENDENT VARIABLES
	local Y : word count $depvars_tab4_iv // PICKS EACH VARIABLE ONE BY ONE FROM MACRO
	forvalues i=1/`Y' {
		local y : word `i' of $depvars_tab4
		`cmd' `y' ${spec`spec'} // THIS RUNS THE REGRESSION
		estadd ysumm
		
		mat b = e(b) // THIS SAVES THE COEFFICIENT VECTOR
		scalar bPM10_`spec'_`i'=b[1,1] // PM10 coefficient
		
		mat V = e(V) // THIS SAVES THE VARIANCE MATRIX
		
		// THE FOLLOWING SAVES STANDARD ERRORS, R2 AND N AS SCALARS
		scalar sePM10_`spec'_`i'  =sqrt(V[1,1]) // PM10 SE
		scalar r2`spec'_`i'  = e(r2) // r-squared
		scalar N`spec'_`i'  = e(N) // N
		scalar YM`spec'_`i' = e(ymean)  // Mean Dep. Var.
		scalar CDF`spec'_`i' = e(cdf) // Cragg-Donald Wald F statistic
		scalar KPF`spec'_`i' = e(rkf) // Kleibergen-Paap rk Wald F statistic
		scalar KPLM`spec'_`i'  = e(idstat) // Kleibergen-Paap rk LM statistic (unteridentification)
		
		// THE FOLLOWING PRODUCES SIGNIFICANCE STARS AND SAVES THEM AS LOCALS
		local pPM10_`spec'_`i'=2*(1-normal(abs(bPM10_`spec'_`i'/sePM10_`spec'_`i')))
		local starPM10_`spec'_`i' ""
		if `pPM10_`spec'_`i''<.1 {	
			local starPM10_`spec'_`i' *
		}
		if `pPM10_`spec'_`i''<.05 {
			local starPM10_`spec'_`i' **
		}
		if `pPM10_`spec'_`i''<.01 {
			local starPM10_`spec'_`i' ***
		}
		global starPM10_`spec'_`i' = "`starPM10_`spec'_`i''"
	}  // i
end

*------------------------------------------------------------------------------*

***PROGRAM CONTROLLING FOR TEMPERATURE BINS [COL. 2], NO2 [COL. 3], LAGS LEADS [COL.4]
cap program drop reg_tab4_temp_no2
program define reg_tab4_temp_no2, sclass
	args cmd spec 
	
	// THE FOLLOWING LOOP RUNS THE ANALYSIS SEPARATELY FOR ALL DEPENDENT VARIABLES
	local Y : word count $depvars_tab4 // PICKS EACH VARIABLE ONE BY ONE FROM MACRO
	forvalues i=1/`Y' {
		local y : word `i' of $depvars_tab4
		`cmd' `y' ${spec`spec'} // THIS RUNS THE REGRESSION
		estadd ysumm
		
		mat b = e(b) // THIS SAVES THE COEFFICIENT VECTOR
		scalar bPM10_`spec'_`i'=b[1,1] // PM10 coefficient
		
		mat V = e(V) // THIS SAVES THE VARIANCE MATRIX
		
		// THE FOLLOWING SAVES STANDARD ERRORS, R2 AND N AS SCALARS
		scalar sePM10_`spec'_`i'  =sqrt(V[1,1]) // PM10 SE
		scalar r2`spec'_`i'  = e(r2) // r-squared
		scalar N`spec'_`i'  = e(N) // N
		scalar YM`spec'_`i' = e(ymean)  // Mean Dep. Var.
		
		// THE FOLLOWING PRODUCES SIGNIFICANCE STARS AND SAVES THEM AS LOCALS
		local pPM10_`spec'_`i'=2*(1-normal(abs(bPM10_`spec'_`i'/sePM10_`spec'_`i')))
		local starPM10_`spec'_`i' ""
		if `pPM10_`spec'_`i''<.1 {	
			local starPM10_`spec'_`i' *
		}
		if `pPM10_`spec'_`i''<.05 {
			local starPM10_`spec'_`i' **
		}
		if `pPM10_`spec'_`i''<.01 {
			local starPM10_`spec'_`i' ***
		}
		global starPM10_`spec'_`i' = "`starPM10_`spec'_`i''"
	}  // i
end

*------------------------------------------------------------------------------*

***PROGRAM FOR ASYMMETRIC EFFECTS [COL. 6]
cap program drop reg_tab4_asym
program define reg_tab4_asym, sclass
	args cmd spec 
	
	// THE FOLLOWING LOOP RUNS THE ANALYSIS SEPARATELY FOR ALL DEPENDENT VARIABLES
	local Y : word count $depvars_tab4 // PICKS EACH VARIABLE ONE BY ONE FROM MACRO
	forvalues i=1/`Y' {
		local y : word `i' of $depvars_tab4
		`cmd' `y' ${spec`spec'} // THIS RUNS THE REGRESSION
		estadd ysumm
		
		mat b = e(b) // THIS SAVES THE COEFFICIENT VECTOR
		scalar bPM10q1_`spec'_`i'=b[1,1] // PM10 in q1 coefficient
		scalar bPM10q4_`spec'_`i'=b[1,2] // PM10 in q4 coefficient
		
		mat V = e(V) // THIS SAVES THE VARIANCE MATRIX
		
		// THE FOLLOWING SAVES STANDARD ERRORS, R2 AND N AS SCALARS
		scalar sePM10q1_`spec'_`i'  =sqrt(V[1,1]) // PM10 in q1 SE
		scalar sePM10q4_`spec'_`i'  =sqrt(V[2,2]) // PM10 in q4 SE
		scalar r2`spec'_`i'  = e(r2) // r-squared
		scalar N`spec'_`i'  = e(N) // N
		scalar YM`spec'_`i' = e(ymean)  // Mean Dep. Var.
		
		// THE FOLLOWING PRODUCES SIGNIFICANCE STARS AND SAVES THEM AS LOCALS
		forval q = 1(3)4 {
			local pPM10q`q'_`spec'_`i'=2*(1-normal(abs(bPM10q`q'_`spec'_`i'/sePM10q`q'_`spec'_`i')))
			local starPM10q`q'_`spec'_`i' ""
			if `pPM10q`q'_`spec'_`i''<.1 {	
				local starPM10q`q'_`spec'_`i' *
			}
			if `pPM10q`q'_`spec'_`i''<.05 {
				local starPM10q`q'_`spec'_`i' **
			}
			if `pPM10q`q'_`spec'_`i''<.01 {
				local starPM10q`q'_`spec'_`i' ***
			}
			global starPM10q`q'_`spec'_`i' = "`starPM10q`q'_`spec'_`i''"
		}  // q
	}  // i
end

*------------------------------------------------------------------------------*


***Macros
global depvars_tab4 "Incumbent_share"
global depvars_tab4_iv "Incumbent_share"
global depvars_tab4_votes "Est_opp_share OtherParties_share GRUNE_share Invalid_share"
global weather_controls "O3_30km_10ug temp_30km humid_rel_30km wind_speed_30km precip_30km"
global weather_controls_bins "O3_30km_10ug temp_30km_bin1-temp_30km_bin8 humid_rel_30km wind_speed_30km precip_30km"
global weather_controls_no2 "O3_30km_10ug NO2_30km_10ug temp_30km humid_rel_30km wind_speed_30km precip_30km"
global demo_econ_controls "pop_tot GDP_capita employment"
global clust_opt "cluster(kreis_code)"
*Lags and leads for Col. 4
local ll "lag lead"
global lags_leads ""
foreach w in PM10_10ug O3_10ug temp humid_rel wind_speed precip {
	foreach l of local ll {
		forval t = 1/3 {
			global lags_leads "${lags_leads} `l'_`w'_`t'_30km "
		}
	}
}



// THE FOLLOWING ARE ALL THE SPECIFICATIONS WE RUN (X-VARIABLES PLUS OPTIONS)

// Specifications in 10s of micrograms
	// IV inbumbent. PM10 with Ozone and weather controls, demographic and economic controls, and municipality, date and type fixed effects
	global spec1 "$weather_controls $demo_econ_controls turnout (PM10_30km_10ug=iv_*) i.type, $absorb_opt $clust_opt, if samplePM10_allcontrols == 1"
	// Incumbent with temperatue bins
	global spec2 "PM10_30km_10ug $weather_controls_bins $demo_econ_controls i.type, ${hdfe_opt}, if samplePM10_allcontrols == 1"
	// Incumbent with NO2 control
	global spec3 "PM10_30km_10ug $weather_controls_no2 $demo_econ_controls turnout i.type, ${hdfe_opt}, if samplePM10_allcontrols == 1"
	// Established opposition and other parties. PM10 with Ozone and weather controls, demographic and economic controls, and municipality, date and type fixed effects
	global spec4 "PM10_30km_10ug $weather_controls $demo_econ_controls turnout i.type, ${hdfe_opt}, if samplePM10_allcontrols == 1"
	// Incumbent with dummies for election PM10 in Q1 and Q4
	global spec5 "PM10el_q1 PM10el_q4 $weather_controls $demo_econ_controls turnout i.type, ${hdfe_opt}, if samplePM10_allcontrols == 1"

*------------------------------------------------------------------------------*

***Generate dummies for pollution on election days in Q1 and Q4
use "${tempdata}kreise_p_w_electiondates_plusminus30days.dta", clear

*Gen quartiles
egen PM10_4 = xtile(PM10_ug_weight), by(kreis_code) p(25(25)75)

*Gen dummies for election pollution in Q1 and Q4
gen PM10el_q1 = 1 if PM10_4 == 1 & el ==1
gen PM10el_q4 = 1 if PM10_4 == 4 & el ==1
replace PM10el_q1 = 0 if PM10el_q1 == .
replace PM10el_q4 = 0 if PM10el_q4 == .

*Prepare for merge
keep if el == 1
keep kreis_code date PM10el_q1 PM10el_q4
rename date election_date

*Save
save "${tempdata}PM10_el_q1q4.dta", replace

*------------------------------------------------------------------------------*

use Incumbent_share Est_opp_share OtherParties_share GRUNE_share turnout Eligible_voters Valid PM10_30km_10ug O3_30km_10ug NO2_ug_weight_30km temp_30km humid_rel_30km wind_speed_30km precip_30km pop_tot GDP_capita employment kreis_code election_date type $lags_leads samplePM10_allcontrols using "${tempdata}kreise_complete_all.dta", clear

*------------------------------------------------------------------------------*

***Merge datasets
**Wind instruments --> Potentially change to "${tempdata}wind_rb_d_20002019_iv.dta" if we produce it via 1_wind_iv.do
merge m:1 kreis_code election_date using "${tempdata}wind_rb_d_20002019_iv.dta", keep(master match) nogen

**Dummies for election pollution in Q1 and Q4
merge m:1 kreis_code election_date using "${tempdata}PM10_el_q1q4.dta", keep(master match) nogen


*------------------------------------------------------------------------------*

***Assign wind directions to Hannover Landeshauptstadt and Saarbrucken Landeshauptstadt
*Hannover Landeshauptstadt
foreach el in 27sep2009 20jan2013 22sep2013 24sep2017 15oct2017 {
	*foreach var of varlist iv_bl1_d1-iv_bl4_d4 {
	foreach var of varlist iv_rb10_d1-iv_rb160_d4 {
		sum `var' if (kreis_code == 3241 & election_date == td(`el'))  // Region Hannover, Landkreis
		scalar `var'_3241_`el' = r(mean)
		replace `var' = `var'_3241_`el' if (kreis_code == 3241001 & election_date == td(`el'))
	} // var
} // el

*Saarbrucken Landeshauptstadt
foreach el in 25mar2012 22sep2013 26mar2017 24sep2017 {
	*foreach var of varlist iv_bl1_d1-iv_bl4_d4 {
	foreach var of varlist iv_rb10_d1-iv_rb160_d4 {
		sum `var' if (kreis_code == 10041 & election_date == td(`el'))  // Saarbrücken, Regionalverband
		scalar `var'_10041_`el' = r(mean)
		replace `var' = `var'_10041_`el' if (kreis_code == 10041100 & election_date == td(`el'))
	} // var
} // el

*Aachen, krfr. Stadt --> Only rb dataset
foreach el in 14may2000 22sep2002 22may2005 18sep2005 27sep2009 09may2010 13may2012 22sep2013 14may2017 24sep2017 {
	foreach var of varlist iv_rb10_d1-iv_rb160_d4 {
		sum `var' if (kreis_code == 5334 & election_date == td(`el'))  // Region Hannover, Landkreis
		scalar `var'_5334_`el' = r(mean)
		replace `var' = `var'_5334_`el' if (kreis_code == 5334002 & election_date == td(`el'))
	} // var
} // el

*------------------------------------------------------------------------------*

***Generate temperature bins
gen temp_30km_bins = 1 if temp_30km < -5 & temp_30km != .
replace temp_30km_bins = 2 if temp_30km >= -5 & temp_30km < 0
replace temp_30km_bins = 3 if temp_30km >= 0 & temp_30km < 5
replace temp_30km_bins = 4 if temp_30km >= 5 & temp_30km < 10
replace temp_30km_bins = 5 if temp_30km >= 10 & temp_30km < 15
replace temp_30km_bins = 6 if temp_30km >= 15 & temp_30km < 20
replace temp_30km_bins = 7 if temp_30km >= 20 & temp_30km < 25
replace temp_30km_bins = 8 if temp_30km >= 25 & temp_30km != .
tabulate temp_30km_bins, gen(temp_30km_bin)

*------------------------------------------------------------------------------*

***Generate NO2 in tens of micrograms
gen NO2_30km_10ug = NO2_ug_weight_30km / 10

*------------------------------------------------------------------------------*

***Generate invalid and share of invalid
gen Invalid = ( Eligible_voters * turnout ) - Valid
replace Invalid = 0 if Invalid > -1 & Invalid < 0
replace Invalid = . if Invalid < -1
gen Invalid_share = Invalid / ( Eligible_voters * turnout )

*------------------------------------------------------------------------------*

***Regressions

*IV for incumbent
qui reg_tab4_IV ivreghdfe 1

*Incumbent with temperature bins and NO2
forvalues j=2/3 {
qui reg_tab4_temp_no2 reghdfe `j'
}

*Established opposition and Other parties
qui reg_tab4_votes reghdfe 4

*Asymmetric effects
qui reg_tab4_asym reghdfe 5

*------------------------------------------------------------------------------*

***Generate table
gen str table=""


// PM10 coefficients
local i=1
local line "PM10 ($10\mu g / m^3$)&"
local line "`line'" $f_b bPM10_1_1 "\sym{$starPM10_1_1}" "&" $f_b bPM10_2_1 "\sym{$starPM10_2_1}" "&" $f_b bPM10_3_1 "\sym{$starPM10_3_1}" "&"  "&" $f_b bPM10_4_1 "\sym{$starPM10_4_1}" "&" $f_b bPM10_4_2 "\sym{$starPM10_4_2}" "&" $f_b bPM10_4_3 "\sym{$starPM10_4_3}" "&" $f_b bPM10_4_4 "\sym{$starPM10_4_4}" " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// PM10 s.e.
local i=`i'+1
local line "&" "(" $f_se sePM10_1_1 ")" "&" "(" $f_se sePM10_2_1 ")" "&" "(" $f_se sePM10_3_1 ")" "&"  "&" "(" $f_se sePM10_4_1 ")" "&" "(" $f_se sePM10_4_2 ")" "&" "(" $f_se sePM10_4_3 ")" "&" "(" $f_se sePM10_4_4 ")" " \\ " 
local line: display "`line'" 
qui replace table="`line'" in `i'
// PM10 in q1 coefficients
local i=`i'+1
local line "PM10 in Q1 &"
local line "`line'"  "&"  "&"  "&" $f_b bPM10q1_5_1 "\sym{$starPM10q1_5_1}" "&"  "&"  "&"  "&"  " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// PM10 in q1 s.e.
local i=`i'+1
local line "&"  "&"  "&"  "&" "(" $f_se sePM10q1_5_1 ")" "&"  "&"  "&"  "&"  " \\ " 
local line: display "`line'" 
qui replace table="`line'" in `i'
// PM10 in q4 coefficients
local i=`i'+1
local line "PM10 in Q4 &"
local line "`line'"  "&"  "&"  "&" $f_b bPM10q4_5_1 "\sym{$starPM10q4_5_1}" "&"  "&"  "&"  "&"  " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// PM10 in q4 s.e.
local i=`i'+1
local line "&"  "&"  "&"  "&" "(" $f_se sePM10q4_5_1 ")" "&"  "&"  "&"  "&"  " \\ " 
local line: display "`line'" 
qui replace table="`line'" in `i'
// Y mean
local i=`i'+1
local line "Mean dep. var.&"
local line "`line'" $f_YM YM1_1 "&" $f_YM YM2_1 "&" $f_YM YM3_1 "&" $f_YM YM5_1 "&" $f_YM YM4_1 "&" $f_YM YM4_2 "&" $f_YM YM4_3 "&" $f_YM YM4_4 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// R2
local i=`i'+1
local line "R$^2$ & "
local line "`line'" $f_r2 r21_1 "&" $f_r2 r22_1 "&" $f_r2 r23_1 "&" $f_r2 r25_1 "&" $f_r2 r24_1 "&" $f_r2 r24_2 "&" $f_r2 r24_3 "&" $f_r2 r24_4 " \\ " 
local line: display "`line'" 
qui replace table="`line'" in `i'
// Observations
local i=`i'+1
local line "N&"
local line "`line'" $f_N N1_1 "&" $f_N N2_1 "&" $f_N N3_1 "&" $f_N N5_1 "&" $f_N N4_1 "&" $f_N N4_2 "&" $f_N N4_3 "&" $f_N N4_4 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// Kleibergen-Paap rk Wald F statistic
local i=`i'+1
local line "Kleibergen-Paap F&"
local line "`line'" $f_F KPF1_1 "&"  "&"  "&"  "&"  "&"  "&"  "&"  " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// CONTROLS
local i=`i'+1
qui replace table="\multicolumn{9}{l}{\textit{Controls} } \\ " in `i'
local i=`i'+1
qui replace table="\ \ County FE & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ El. Date FE & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ Weather & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ Temperature & linear & dummies & linear & linear & linear & linear & linear & linear \\" in `i'
local i=`i'+1
qui replace table="\ \ Ozone & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ NO2 &  &  & \checkmark &  &  &  &  & \\" in `i'
local i=`i'+1
qui replace table="\ \ Demographics & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ El. Type FE & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark& \checkmark & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ Turnout & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark & \checkmark %\\" in `i'
local i=`i'+1


**Create .tex file
outfile table using "${outputs}table_S4.tex" in 1/`i', replace noquote
drop table
macro drop star*

*------------------------------------------------------------------------------*

clear

exit
